#include <iostream>
using namespace std;
#include <map>
#include <string>

//map 键值对  键是唯一的

//map元素的添加/遍历/删除基本操作
void test1()
{
        map<int, string> map1;
        //添加
        map1.insert(pair<int, string>(1, "teacher01"));
        map1.insert(pair<int, string>(2, "teacher02"));
        //添加
        map1.insert(make_pair(3, "teacher03"));
        map1.insert(make_pair(4, "teacher04"));
         //添加
        map1.insert(map<int, string>::value_type(5, "teacher05"));
        map1.insert(map<int, string>::value_type(6, "teacher06"));
        //添加
        map1[7] = "teacher07";
        map1[8] = "teacher08";

        //容器的遍历
        for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
        {
                cout<<it->first<<" "<<it->second<<endl;
        }

        //容器元素的删除
        while (!map1.empty() )
        {
                map<int, string>::iterator it = map1.begin();
                cout<<it->first <<" "<<it->second<<endl;
                map1.erase(it);
        }
        
}

//插入的四种方法 异同
//前三种方法 返回值为pair<iterator, bool> 若key已经存在 则报错
// 方法四                                                                    若key已经存在 则覆盖原来的                 
void test2()
{
        map<int, string> map1;
        pair<map<int, string>::iterator, bool> mypair1 = map1.insert(pair<int, string>(1, "teacher01"));
        if(mypair1.second == true)
        {
                cout<<"key 1 插入成功"<<endl;
        }else{
                cout<< mypair1.first->first<< " " << mypair1.first->second <<"插入失败"<< endl;
        }
        pair<map<int, string>::iterator, bool> mypair2 = map1.insert(make_pair(2, "teacher02"));
        if(mypair2.second == true)
        {
                cout<<"key 2 插入成功"<<endl;
        }else{
                cout<< mypair2.first->first<< " " << mypair2.first->second <<"插入失败"<< endl;
        }
        pair<map<int, string>::iterator, bool> mypair3 = map1.insert(map<int, string>::value_type(3, "teacher03"));
        if(mypair3.second == true)
        {
                cout<<"key 3 插入成功"<<endl;
        }else{
                cout<< mypair3.first->first<< " " << mypair3.first->second <<"插入失败"<< endl;
        }

        //添加
        map1[4] = "teacher04";
        map1[4] = "teacher44"; //覆盖

        //容器元素的删除
        while (!map1.empty() )
        {
                map<int, string>::iterator it = map1.begin();
                cout<<it->first <<" "<<it->second<<endl;
                map1.erase(it);
        }
}

//map的查找
void test3()
{
        map<int, string> map1;

        map1.insert(pair<int, string>(1, "teacher01"));
        map1.insert(pair<int, string>(2, "teacher02"));
        map1.insert(make_pair(3, "teacher03"));
        map1.insert(make_pair(4, "teacher04"));
        map1.insert(map<int, string>::value_type(5, "teacher05"));
        map1.insert(map<int, string>::value_type(6, "teacher06"));
        map1[7] = "teacher07";
        map1[8] = "teacher08";

        for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
        {
                cout<<it->first<<" "<<it->second<<endl;
        }

        //map.find查找 异常处理
        map<int,string>::iterator it2 =  map1.find(100);
        if(it2 == map1.end())
        {
                cout<<"key 100 的值 不存在"<<endl;
        }
        else
        {
                cout<<it2->first<<" "<<it2->second<<endl;
        }

        //map.equal_range 查找返回前面和后面两个迭代器 异常处理
        pair<map<int, string>::iterator,map<int, string>::iterator > pair1 =  map1.equal_range(5);
        //第一个迭代器 >=5的位置
        //第二个迭代器 >5的位置
        if(pair1.first == map1.end())
        {
                cout<<"第一个迭代器 >=5 的 位置 不存在"<<endl;
        }
        else
        {
                cout<<pair1.first->first<<" "<<pair1.first->second<<endl;
        }

        if(pair1.second == map1.end())
        {
                cout<<"第二个迭代器 >5 的 位置 不存在"<<endl;
        }
        else
        {
                cout<<pair1.second->first<<" "<<pair1.second->second<<endl;
        }
}

int main(int argc, char const *argv[])
{
        //test1();
        //test2();
        test3();     
        return 0;
}
